package ui.swing.filechoose;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FileChooserDemo1 extends JFrame implements ActionListener {

	private static final long serialVersionUID = -714778009439727790L;

	JLabel label = null;
	JTextArea textArea = null;
	JFileChooser fileChooser = null;
	
	public FileChooserDemo1() {
		super("FileChoose Example");
		Dimension scrDimension = getToolkit().getScreenSize();
		setBounds((int)scrDimension.getWidth()/2 - 800/2, (int)scrDimension.getHeight()/2 - 600/2, 800, 600);
		
		textArea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setPreferredSize(new Dimension(350, 300));
		
		JPanel panel = new JPanel();
		JButton b1 = new JButton("打开文件");
		b1.addActionListener(this);
		JButton b2 = new JButton("存储文件");
		b2.addActionListener(this);
		panel.add(b1);
		panel.add(b2);
		
		label = new JLabel(" ", JLabel.CENTER);

		// 建立一个FileChooser对象，并指定D:为默认文件对话框路径
		fileChooser = new JFileChooser("D:\\");
		
		getContentPane().add(label, BorderLayout.NORTH);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		getContentPane().add(panel, BorderLayout.SOUTH);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		File file = null;
		int result;
		
		/* 当用户按下"打开文件"按钮时,JFileChooser的showOpenDialog()方法会输出文件对话框,并利用
		 * setApproveButtonText()方法取代按钮上"Open"文字;以setDialogTitle()方法设置打开文件对话框Title名称
		 * 当使用选择完后,会将选择结果存到result变量中
		 **/
		if("新建文件".equals(e.getActionCommand())) {
			fileChooser.setApproveButtonText("确定");
			fileChooser.setDialogTitle("打开文件");
			result = fileChooser.showOpenDialog(this); // 父窗口就是本Frame
			
			textArea.setText("");
			/* 当用户按下打开文件对话框的"确定"钮后,我们就可以利用getSelectedFile()方法取得文件对象.若是用户按下打
             * 开文件对话框的"Cancel"钮,则将在label上显示"你没有选择任何文件"字样.
             */
			if (result == JFileChooser.APPROVE_OPTION) {
				file = fileChooser.getSelectedFile();
				label.setText("您选择打开的文件名为:" + file.getName());
			}
			else if (result == JFileChooser.CANCEL_OPTION)
			{
				label.setText("您没有选择任何文件");
			}

			if( file != null ) {
				
				FileInputStream fis = null;
				InputStreamReader isr = null;
				
				try {
					fis = new FileInputStream(file);
					isr = new InputStreamReader(fis);
					char[] cbuff = new char[4096];
					int bytesReaded;
					while((bytesReaded = isr.read(cbuff, 0, 4096)) != -1) {
						textArea.append(new String(cbuff, 0, bytesReaded));
					}
					
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
					label.setText("FileNotFoundException");
				} catch (IOException ex) {
					ex.printStackTrace();
					label.setText("IOException");
				} finally {
					if(fis!=null)
						try {fis.close();} catch(IOException ex){};
					if(isr!=null)
						try {isr.close();} catch(IOException ex){};
				}
			}
		}
		if("存储文件".equals(e.getActionCommand())) {
			result = fileChooser.showSaveDialog(this);
			if(result == JFileChooser.APPROVE_OPTION)
			{
				file = fileChooser.getSelectedFile();
				label.setText("储存到文件:" + file.getName());
			}
			else if (result == JFileChooser.CANCEL_OPTION)
			{
				label.setText("操作取消");
			}
			
			if(file!=null)
			{
				FileOutputStream fos = null;
				try {
					fos = new FileOutputStream(file);
					fos.write(textArea.getText().getBytes());
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException ex) {
					ex.printStackTrace();
				} finally {
					if(fos!=null)
						try {fos.close();} catch(IOException ex){};
				}				
			}
		}
	}
	
	public static void main(String[] args) {
		new FileChooserDemo1();
	}

}
